package org.stvd.controller.message;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stvd.common.aspectj.lang.annotation.Log;
import org.stvd.common.aspectj.lang.enums.BusinessType;
import org.stvd.common.enums.MessageStatusEnum;
import org.stvd.common.enums.MessageTypeEnum;
import org.stvd.common.security.SecurityUserHolder;
import org.stvd.common.utils.InterfaceResult;
import org.stvd.controller.BaseController;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.util.StringUtil;
import org.stvd.core.util.json.JsonUtils;
import org.stvd.entities.message.BsMessageLog;
import org.stvd.service.message.BsMessageLogService;

import net.sf.json.JSONObject;

/**
 * @Title: BsMessageController
 * @Description: 接收消息控制类
 * @author houzx
 * @date 2020年10月10日
 */
@Controller
@RequestMapping("message/receive")
public class BsReceiveMessageController extends BaseController{

    @Autowired
    private BsMessageLogService bsMessageLogService;
    
    private String tmpURL = "/message/receive/";
    
    /**
     * @Description: 接收消息管理
     * @param bsMessageLog
     * @return
     */
    @RequestMapping(value = "manage")
    public String onMessageManage(@ModelAttribute(value="bsMessageLog") BsMessageLog bsMessageLog){
        if(StringUtil.isEmpty(bsMessageLog.getRecId())) {
            bsMessageLog.setRecId(SecurityUserHolder.getCurrentUserId());
        }
        QueryResult<Map<String, Object>> queryResult = bsMessageLogService.queryMessageResult(
            (pageNo - 1) * pageSize, pageSize, bsMessageLog.getRecId(), bsMessageLog.getType(), bsMessageLog.getStatus());
        map.put("queryResult", queryResult);
        map.put("messageTypeMap", MessageTypeEnum.messageTypeMap);
        map.put("messageStatusMap", MessageStatusEnum.messageStatusMap);
        return tmpURL + "BsReceiveMessageManage.html";
    }
    protected void loadParameterData(HttpServletRequest request, JSONObject jsonMap) {
        BsMessageLog bsMessageLog = new BsMessageLog();
        map.put("bsMessageLog", JsonUtils.textToBean(bsMessageLog, String.valueOf(jsonMap)));
    }
    
    /**
     * @Description: 消息详情
     * @param logId
     * @return
     */
    @GetMapping(value = "detail/{logId}")
    public String onMessageDetail(@PathVariable Long logId) {
        Map<String, Object> bsMessageMap = bsMessageLogService.getMessageByLogId(logId);
        map.put("bsMessageMap", bsMessageMap);
        return tmpURL + "BsReceiveMessageDetail.html";
    }
    
    /**
     * @Description: 消息已阅
     * @return
     */
    @Log(title = "消息已阅操作", businessType =  BusinessType.UPDATE)
    @PostMapping(value = "read")
    @ResponseBody
    public InterfaceResult<Map<String, Object>> onMessageRead(String logId) {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        bsMessageLogService.updateMessageStatus(logId, MessageStatusEnum.IsRead.getValue());
        result.setMsg("请求成功");
        return result;
    }
    
    /**
     * @Description: 消息批量操作
     * @return
     */
    @Log(title = "消息批量操作", businessType =  BusinessType.UPDATE)
    @PostMapping(value = "batch")
    @ResponseBody
    public InterfaceResult<Map<String, Object>> onMessageBatch() {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        String logIds = request.getParameter("logIds");
        String status = request.getParameter("status");
        bsMessageLogService.updateMessageStatus(logIds, status);
        result.setMsg("操作成功");
        return result;
    }
    
    /**
     * @Description: 获取消息数量
     * @return
     */
    @ResponseBody
    @GetMapping(value = "count")
    public Map<String, Object> onCountUserNotify(){
        Map<String, Object> data = new HashMap<>();
        String userId = SecurityUserHolder.getCurrentUserId();
        Long countMessage = bsMessageLogService.countUnreadMessage(userId);
        data.put("countMessage", countMessage);
        return data;
    }
}
